package controller;

import org.apereo.cas.services.RegexRegisteredService;
import org.apereo.cas.services.RegisteredService;
import org.apereo.cas.services.ReturnAllAttributeReleasePolicy;
import org.apereo.cas.services.ServicesManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URL;

@Controller
public class DynamicServiceController {

    private Logger logger = LoggerFactory.getLogger(DynamicServiceController.class);

    @RequestMapping("/goDynamicService")
    public String goDynamicService(){
        return "DynamicService/dynamicService.html";
    }

    // 这个是管理我们所有服务的类
    @Autowired
    @Qualifier("servicesManager")
    private ServicesManager servicesManager;

 /*   @GetMapping("/addClient/{serviceId}")
    public String addClient(@PathVariable("serviceId") String serviceId) throws IOException {
        String a="^(https|imaps|http)://"+serviceId+".*";
        RegexRegisteredService service=new RegexRegisteredService();
        ReturnAllAttributeReleasePolicy re=new ReturnAllAttributeReleasePolicy();
        service.setServiceId(a);
//        service.setId(id);
        service.setAttributeReleasePolicy(re);
        service.setName("app name");
        service.setLogoutUrl(new URL("http://"+serviceId));//这个是为了单点登出而作用的
        servicesManager.save(service);
        servicesManager.load();
        return "success";
    }*/
    @PostMapping("/addClient")
    public String addClient(@RequestParam(value = "serviceId" ,required = true) String serviceId,
                            @RequestParam(value = "name" ,required = true) String name) throws IOException {
//       String a="^(https|imaps|http)://"+serviceId+".*";
        RegexRegisteredService service=new RegexRegisteredService();
        ReturnAllAttributeReleasePolicy re=new ReturnAllAttributeReleasePolicy();
        service.setServiceId(serviceId);
        service.setAttributeReleasePolicy(re);
        service.setName(name);
//        service.setLogoutUrl(new URL("http://"+serviceId));//这个是为了单点登出而作用的
        servicesManager.save(service);
        servicesManager.load();
        return "success";
    }

    @PostMapping("/delClient")
    public String delClient(@RequestParam(value = "id" ,required = true) int id) throws IOException {

            try {

                Long longId = Long.parseLong(Integer.toString(id));
                RegisteredService delete = servicesManager.delete(longId);
                System.out.println("DELETE ServiceId :********************\n\t " + delete.getServiceId());
                return "success";
            } catch (Exception e) {
                e.printStackTrace();
               /* System.out.println("delClient ：id 错误");
                logger.info("delClient ：id 错误");*/
                return "error";
            }

    }
}
